<template>
	<view class="wrap" :selectable="false">
		<view class = "searchSchool">
			<view class = "searchInput">
				<u-search placeholder="请输入想要查看的学校" v-model="schoolName" :show-action="false" @search="onSearch"></u-search>
			</view>
			<view class="school-position">
				<u-icon name="map" size="35" color="#A9A9A9" top="3"></u-icon>
				<text class = "school-name">{{school}}</text>
			</view>
		</view>
		<view class="notice">
			<u-notice-bar mode="vertical" :list="noticeList" type="none" :speed="180" :duration="3000"></u-notice-bar>
		</view>
		<view class="wrap-banner">
			<u-swiper :list="bannerList"></u-swiper>
		</view>
		<template v-for="item in taskList">
			<home-item :avater="item.imageUrl" :userName="item.nikeName" :school="item.school" :sex="item.sex == 0 ? 'woman':'man'"
			 :postDate="item.publishTime" status="待接单" :title="item.taskTitle" :fromPlace="item.pickUpAddress" :toPlace="item.toAddress"
			 :price="item.moneyReward" @click="onHomeItemClick(item.taskId)"></home-item>
		</template>
		
		<!-- 全局message组件 -->
		<quick-message ref="message"></quick-message>
		
		<u-loadmore :status="status" :load-text="loadText" @loadmore="loadMore" />
		<u-tabbar :list="vuex_tabbar" :mid-button="true" @change="vuex_onTabChange"></u-tabbar>
	</view>
</template>

<script>
	/*
	 * 作者: 臧锡洋
	 * 时间: 2020年10月29日 22:49:39
	 * 版本: V1
	 * 描述: 小程序首页 - 任务大厅
	 */
	export default {
		/* 下拉刷新 */
		onLoad: function(options) {
			key = encodeURIComponent(options)
			console.log(key)
			uni.startPullDownRefresh();
		},
		onPullDownRefresh() {
			console.log('开始下拉刷新');
			// 默认获取第一页内容
			this.pagination.current = 1
			this.$u.api.getTasks(this.pagination.current, this.pagination.size).then(res => {
				console.log(res.data.list)
				this.taskList = []
				this.taskList = res.data.list
				// 是否有下一页
				this.pagination.hasNextPage = res.data.hasNextPage

				// 获取通知信息
				this.$u.api.getNotice().then(res => {
					console.log(res.data)
					for (let index in res.data) {
						// 压入noticeList中
						this.noticeList.push(res.data[index].msg)
					}
					this.$u.api.getBanner().then(res => {
						console.log(res.data)
						this.bannerList = res.data
						uni.stopPullDownRefresh() // 停止动画
					})
				})

			})

		},
		onReachBottom() {
			console.log('开始上拉刷新')
			if (this.pagination.hasNextPage) {
				// 存在下一页
				this.status = 'loading'
				this.$u.api.getTasks(++this.pagination.current, this.pagination.size).then(res => {
					for (let i in res.data.list) {
						this.taskList.push(res.data.list[i])
					}
					this.pagination.hasNextPage = res.data.hasNextPage
					this.status = 'loadmore'
				})
			} else {
				this.status = 'nomore'
			}
		},
		data() {
			return {
				show: false,
				content: '加载中...',
				avatar: '',
				bannerList: [],
				noticeList: [],
				pagination: {
					current: 1,
					size: 5,
					hasNextPage: false
				},
				taskList: [],
				status: 'loadmore',
				loadText: {
					loadmore: '轻轻上拉',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				refreshFlag: false,
				school:"所有学校",
				schoolName:" ",
			}
		},
		methods: {
			getSwipers() {

				this.$u.api.getBanner().then(res => {
					this.bannerList = res.data
				}).catch(err => {
					console.log("失败" + JSON.stringify(err))
				})
			},
			// 获取当前消息通知
			getNotice() {
				this.$u.api.getNotice().then(res => {
					for (let index in res.data) {
						// 压入noticeList中
						this.noticeList.push(res.data[index].msg)
					}

				})
			},
			onHomeItemClick(id) {
				console.log('被点击')
				uni.navigateTo({
					url: `../functionPages/taskDetail/taskDetail?id=${id}&from=home`
				});
			},
			testAuth() {
				this.$u.api.testAuth().then(res => {
					console.log(res)
				})
			},
			// 获取待接受的任务
			getTasksPedding() {
				this.$u.api.getTasks(this.pagination.current,this.pagination.size,this.schoolName).then(res => {
					console.log("接受任务请求成功",res.data.list)
					this.taskList = res.data.list //赋值
					// 是否有下一页
					this.pagination.hasNextPage = res.data.hasNextPage
				})
			},
			onSearch(){
				this.$u.api.getTasks(this.pagination.current,this.pagination.size,this.schoolName).then(res => {
					console.log("搜索请求发起！",res.data)
					this.taskList = res.data.list //赋值
					// 是否有下一页
					this.pagination.hasNextPage = res.data.hasNextPage
				})
			},
			// 加载更多
			loadMore() {

			}
			
		},
		mounted() {
			// 获取通知
			this.getNotice()
			// 获取banner
			this.getSwipers()
			// 获取任务
			this.getTasksPedding()


			/* 测试message组件 */
			/* this.$refs.message.show({
				type: 'success', //String 默认default
				msg: '点击了消息提示窗', //String 显示内容 *
				direction: 'center', //String 默认顶部显示
				icon: 'success', //Boolean|String 显示icon(false/true/string 默认显示icon)
				mask: true, //Boolean 遮罩（默认false没有遮罩）
				time: 5000, //Number|Boolean 默认3000/false为不自动关闭
				iconSize: 16, //Number 自定义icon大小(单位px 默认16 设置后会覆盖自定义样式里的设置优先级最高)
				iconColor: '', //String icon颜色(默认主题颜色 设置后会覆盖自定义样式里的设置优先级最高)
			}) */
		}
	}
</script>

<style lang="scss" scoped>
	.wrap-banner {
		padding: 20px 40rpx;
	}
	.searchSchool{
		width: 100%;
		height: 40px;
		background-color: #fff;
		display: flex;
		justify-content: space-evenly;
		line-height: 40px;
		.searchInput{
			width: 65%;
			height: 80%;
			color:#A9A9A9;
			.searchValue{
				margin-left: 10px;
			}
		}
		.school-position{
			margin-top: 5px;
			width: 25%;
			height: 80%;
			line-height: 32px;
		}
	}
</style>
